package base.util;

import org.junit.Test;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil {
	
    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";

	public static void main(String[] args) throws Exception {
		LocalDate kprq1 = LocalDate.parse("2022-01-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		LocalDate kprq2 = LocalDate.parse("2022-02-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		LocalDate beforeNMonthDay = kprq2.minusMonths(1);
		System.out.println(isEqual(kprq1, beforeNMonthDay));
	}

	/**
	 * 判断是否同一日期，比如是否同一年，是否同一个月，是否同一天
	 * @param before
	 * @param after
	 * @return
	 */
	public static boolean isEqual(LocalDate before, LocalDate after){
		return before.isEqual(after);
	}

	/**
	 * after 日期是否在 before 之后
	 * @param after
	 * @param before
	 * @return
	 */
	public static boolean isAfter(LocalDate after, LocalDate before){
		return after.isAfter(before);
	}

	/**
	 * before 日期是否在 after 之前
	 * @param before
	 * @param after
	 * @return
	 */
	public static boolean isBefore(LocalDate before, LocalDate after){
		return before.isBefore(after);
	}

	/**
	 * 获取前或者后 years 年的今天，比如今天是2022-01-14，前一年的今天是2021-01-14，后一年的今天是2023-01-14
	 * @param years
	 * @param flag "+" 往后推算， “-” 往前推算
	 * @return
	 */
	public static LocalDate beforeNYearsDay(String flag, int years){
		if (!StringUtils.isEmpty(flag)) {
			switch (flag){
				case "-":
					return LocalDate.now().minusYears(years);
				case "+":
					return LocalDate.now().plusYears(years);
				default:
					return LocalDate.now().minusYears(years);
			}
		}else{
			return LocalDate.now().minusYears(years);
		}
	}

	@Test
	public static String format(Date date) { 
		  long delta = new Date().getTime() - date.getTime();
	      if (delta < 1L * ONE_MINUTE) {
	         long seconds = toSeconds(delta);
	         return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
	      }
	      if (delta < 45L * ONE_MINUTE) {
	         long minutes = toMinutes(delta);
	         return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
	      }
	      if (delta < 24L * ONE_HOUR) {
	         long hours = toHours(delta);
	         return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
	      }
	      if (delta < 48L * ONE_HOUR) {
	         return "昨天";
	      }
	      if (delta < 30L * ONE_DAY) {
	         long days = toDays(delta);
	         return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
	      }
	      if (delta < 12L * 4L * ONE_WEEK) {
	         long months = toMonths(delta);
	         return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
	      } else {
	         long years = toYears(delta);
	         return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
	      }
	}
	
	private static long toSeconds(long date) {
	      return date / 1000L;
	   }

	   private static long toMinutes(long date) {
	      return toSeconds(date) / 60L;
	   }

	   private static long toHours(long date) {
	      return toMinutes(date) / 60L;
	   }

	   private static long toDays(long date) {
	      return toHours(date) / 24L;
	   }

	   private static long toMonths(long date) {
	      return toDays(date) / 30L;
	   }

	   private static long toYears(long date) {
	      return toMonths(date) / 365L;
	   }



}
